<?php
namespace app\controllers\api\account;

use fate\http\Request;
use app\models\User;
use app\libs\LibUser;
use app\libs\Util;

class EditnickController extends \app\controllers\ForceLoginPure {
    public function run() {
        $req = Request::getInstance();

        $uid = LibUser::getLoginUid();
        $nick = Util::plainText($req->getParameter('nick'));

        if(!$nick) {
            $this->ajaxReturn(null, 300002);
            exit;
        }

        $user = new User();
        $has = $user->count("user_name='{$nick}'") > 0;
        if($has) {
            $this->ajaxReturn(null, 100005);
            exit;
        }

        $user->update($uid, ['user_name' => $nick]);

        $this->ajaxReturn(null);
    }
}
